import { createRouter, createWebHistory } from 'vue-router';
import accountApi from '../api/account.js';

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/login',
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/LoginView.vue'),
        },
        {
            path: '/home',
            name: 'home',
            component: () => import('../views/PageIndexView.vue'),
            redirect: '/home/overview',
            children: [
                {
                    path: 'overview',
                    name: 'overview',
                    component: () => import('../views/pages/Overview.vue'),
                },
                {
                    path: 'enterprise',
                    name: 'enterprise',
                    component: () => import('../views/pages/Enterprise.vue'),
                },
                {
                    path: 'dataManage',
                    name: 'dataManage',
                    component: () => import('../views/pages/DataManage.vue'),
                    redirect: '/home/dataManage/all',
                    children: [
                        {
                            path: 'all',
                            name: 'dataManage-all',
                            component: () => import('../views/pages/DataManage.vue'),
                        },
                        {
                            path: 'pending',
                            name: 'dataManage-pending',
                            component: () => import('../views/pages/DataManage.vue'),
                        },
                        {
                            path: 'pending-upload',
                            name: 'dataManage-pendingUpload',
                            component: () => import('../views/pages/DataManage.vue'),
                        },
                    ],
                },
                {
                    path: 'dataAnalysis',
                    name: 'dataAnalysis',
                    component: () => import('../views/pages/DataAnalysis.vue'),
                },
                {
                    path: 'notification',
                    name: 'notification',
                    component: () => import('../views/pages/Notification.vue'),
                    redirect: '/home/notification/all',
                    children: [
                        {
                            path: 'all',
                            name: 'notification-all',
                            component: () => import('../views/pages/Notification.vue'),
                        },
                        {
                            path: 'active',
                            name: 'notification-active',
                            component: () => import('../views/pages/Notification.vue'),
                        },
                        {
                            path: 'expired',
                            name: 'notification-expired',
                            component: () => import('../views/pages/Notification.vue'),
                        },
                        {
                            path: 'draft',
                            name: 'notification-draft',
                            component: () => import('../views/pages/Notification.vue'),
                        },
                    ],
                },
                {
                    path: 'account',
                    name: 'account',
                    component: () => import('../views/pages/Account.vue'),
                },
                {
                    path: 'logs',
                    name: 'logs',
                    component: () => import('../views/pages/Logs.vue'),
                },
                {
                    path: 'survey',
                    name: 'survey',
                    component: () => import('../views/pages/Survey.vue'),
                },
                {
                    path: 'dataExport',
                    name: 'dataExport',
                    component: () => import('../views/pages/dataExport.vue'),
                },
            ],
        },
    ],
});

// 添加全局前置守卫
router.beforeEach(async (to, from, next) => {
    // 除了login路由外，其他路由都需要进行拦截
    if (to.path !== '/login') {
        try {
            // 调用自动登录接口
            let res = await accountApi.autoLogin();
            // 如果返回码为200，则放行
            if (res.code === 200) {
                next();
            } else {
                // 否则跳转到登录页
                next('/login');
            }
        } catch (error) {
            // 发生错误时也跳转到登录页
            console.error('自动登录失败:', error);
            next('/login');
        }
    } else {
        // login路由直接放行
        next();
    }
});

export default router;
